package captcha

import (
	"github.com/wenlng/go-captcha-assets/resources/images"
	"github.com/wenlng/go-captcha-assets/resources/tiles"
	"github.com/wenlng/go-captcha/v2/slide"
	"log"
)

var SlideCapt slide.Captcha

func SetupCaptcha() error {
	//传入配置选项来初始化构建器
	builder := slide.NewBuilder(
		//slide.WithGenGraphNumber(2),
		slide.WithEnableGraphVerticalRandom(true),
	)
	//获取背景图
	imgs, err := images.GetImages()
	if err != nil {
		log.Fatalln(err)
	}
	//获取图形数据
	graphs, err := tiles.GetTiles()
	if err != nil {
		log.Fatalln(err)
	}
	//创建GraphImage类型的切片newGraphs，用来存储处理后的图形数据
	var newGraphs = make([]*slide.GraphImage, 0, len(graphs))
	//遍历原始的图形数据graphs，将每个图形的OverlayImage、MaskImage和ShadowImage属性提取出来，创建新的slide.GraphImage对象并添加到newGraphs切片中
	for i := 0; i < len(graphs); i++ {
		graph := graphs[i]
		newGraphs = append(newGraphs, &slide.GraphImage{
			OverlayImage: graph.OverlayImage,
			MaskImage:    graph.MaskImage,
			ShadowImage:  graph.ShadowImage,
		})
	}
	//将处理后的图形数据newGraphs和背景图片imgs作为资源设置到构建器中
	builder.SetResources(
		slide.WithGraphImages(newGraphs),
		slide.WithBackgrounds(imgs),
	)
	//构建器的Make方法
	SlideCapt = builder.Make()
	return nil
}
